package com.example.iorder.reservation.read;

import java.util.ArrayList;

import com.example.iorder.R;
import com.example.iorder.reservation.Menuitem;
import com.example.iorder.reservation.Reservation;
//import com.example.iorder.reservation.Activity_ShowAllReservations.ResultsItemReservation;
//import com.example.iorder.reservation.Activity_ShowAllReservations.ResultsItemReservation;
//import com.example.iorder.reservation.read.Activity_ShowAllReservations.ListMenuitemAdapter;


import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class ActivityTab_ShowReservationMenuItems  extends ListActivity implements OnScrollListener
{
	private ListMenuitemAdapter mCustomAdapter;
	private static ArrayList<Menuitem> mMenuitemData = new ArrayList<Menuitem>();
	

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// Initializing screen/object
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menuitems_show_all);
		this.setTitle("iOrder | All reservations");
		TextView noMenuitemsView = (TextView) findViewById(R.id.noMenuitems);
		
		Reservation lReservation =  Activity_ShowAllReservations.getSelectedReservation();
		mMenuitemData.clear();
		
		
		for (Menuitem lTmp: lReservation.getMenuitems())
			mMenuitemData.add(lTmp);
		

		if (mMenuitemData.size() <= 0)
		{
			noMenuitemsView.setVisibility(View.VISIBLE);
		}
		else
		{
			noMenuitemsView.setVisibility(View.INVISIBLE);
			
			/*
			 * Reset/reassign the adapter
			 */
			ListView lMenuitemsListView = getListView();
			mCustomAdapter = new ListMenuitemAdapter(ActivityTab_ShowReservationMenuItems.this, mMenuitemData);
			setListAdapter(mCustomAdapter); 
			lMenuitemsListView.setOnScrollListener(ActivityTab_ShowReservationMenuItems.this);				
		}
	}

	
	
	/*
	 * 
	 * 
	 */
	class ListMenuitemAdapter extends BaseAdapter 
	{
		Context context;
		private ArrayList<Menuitem> data;
		int count = 40;
		
		public ListMenuitemAdapter(Context context, ArrayList<Menuitem> data) 
		{
			this.context = context;
			this.data = data;
		}
		
		@Override
		public int getCount() 
		{
			return data.size();
		}

		@Override
		public Object getItem(int position) 
		{
			return data.get(position);
		}

		@Override
		public long getItemId(int position) 
		{
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) 
		{
			// Dummy results
			TextView view = new TextView(ActivityTab_ShowReservationMenuItems.this);
			view.setText("Entry: " + position);
			
			// Custom results with custom view (SearchResultsItem)
			View row = convertView;
			ResultsItemMenuitems itemHolder = new ResultsItemMenuitems();
			
			if (convertView == null) 
			{
				LayoutInflater inflater = ((Activity) context).getLayoutInflater();
				row = inflater.inflate(R.layout.menuitems_show_item, parent, false);
				
				itemHolder.MenuitemTitle = (TextView) row.findViewById(R.id.menuitem_title);
				itemHolder.MenuitemDescription = (TextView) row.findViewById(R.id.menuitem_description);
				
				row.setTag(itemHolder);
			} else {
				itemHolder = (ResultsItemMenuitems) row.getTag();
			}
			
			Menuitem menuitem = null;
			if (position < data.size())
				menuitem = data.get(position);
			
			/*
			 * Assign the GUI elements their value
			 */			
			if (itemHolder != null && menuitem != null) 
			{
				itemHolder.MenuitemTitle.setText(menuitem.getTitle());			
				itemHolder.MenuitemDescription.setText(menuitem.getAmount() + " x " +
													   Float.toString(menuitem.getPrice()) + " = " + 
				                                       (menuitem.getPrice() * menuitem.getAmount()) + " €");
				
			}
			
			return row;
		}
		
		public ArrayList<Menuitem> getRestaurants() { return data; }
	}		
	

	
	
	/*
	 * 
	 * 
	 */
	private static class ResultsItemMenuitems 
	{
		public TextView MenuitemTitle;
		public TextView MenuitemDescription;
	}
	
	
	
	
	/*
	 * 
	 * 
	 */
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) 
	{
		boolean loadMore = firstVisibleItem + visibleItemCount >= totalItemCount;
		
		if (loadMore && visibleItemCount != totalItemCount) 
		{
			mCustomAdapter.count += visibleItemCount;
			mCustomAdapter.notifyDataSetChanged();
		} // if (loadMor...
	}

	
	/*
	 * 
	 * 
	 */
	private void reserveAtSelectedReservation(ResultsItemMenuitems selectedReservation) 
	{
//		AlertDialog.Builder builder = new AlertDialog.Builder(this);
//		String restaurantName = selectedReservation.restName.getText().toString();
//		builder.setMessage("Start reservation at " + restaurantName).setTitle(R.string.alert_dialog_title);
//		builder.setPositiveButton(R.string.lable_button_ok,
//				new DialogInterface.OnClickListener() 
//				{
//					public void onClick(DialogInterface dialog, int id) 
//					{
//						// User clicked OK button
//					}
//				});
//		AlertDialog newDialog = builder.create();
//		newDialog.show();
	}	
	

	/*
	 * 
	 * 
	 */
	@Override
	public void onScrollStateChanged(AbsListView arg0, int arg1) 
	{
		// TODO Auto-generated method stub		
	}	
	
}
